package com.combetter.changxing.viewmodel;

import android.app.Application;
import android.databinding.ObservableField;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.TextView;

import com.combetter.changxing.net.service.APIService;
import com.combetter.changxing.ui.activity.ForgetPwdActivity;
import com.combetter.changxing.util.Check;
import com.combetter.changxing.util.LogUtil;
import com.combetter.changxing.util.RegUtils;
import com.combetter.changxing.util.RxUtil;
import com.combetter.changxing.util.ToastUtils;
import com.combetter.changxing.viewmodel.base.BaseViewModel;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by 深圳唐龙飞讯科技有限公司 on 2018/6/14.
 */

public class ForgetPwdViewModel extends BaseViewModel {
    //手机号码
    public ObservableField<String> userName = new ObservableField<>("");
    //验证码
    public ObservableField<String> code = new ObservableField<>("");
    //密码
    public ObservableField<String> password = new ObservableField<>("");
    //确认密码
    public ObservableField<String> password2 = new ObservableField<>("");


    public ForgetPwdViewModel(@NonNull Application application) {
        super(application);
    }

    public void back(View view) {
        ((ForgetPwdActivity) view.getContext()).finish();
    }


    /**
     * 发送验证码
     */
    public void sendCode(View view) {
        TextView sendCode = (TextView) view;
        if (Check.isEmpty(userName.get())) {
            ToastUtils.error("请输入手机号码");
            return;
        }
        if (!RegUtils.isMobile(userName.get())) {
            ToastUtils.error("手机号码格式不对");
            return;
        }
        mHttpRequest.createService(APIService.class)
                .sendCode(userName.get(), 2)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(httpResult -> {
                    if (httpResult.getCode() == 200) {
                        LogUtil.e("发送验证码成功 " + httpResult.getData());
                        RxUtil.countDown(60).doOnComplete(() -> {
                            sendCode.setEnabled(true);
                            sendCode.setText("发送验证码");
                        }).subscribe(integer -> {
                            sendCode.setEnabled(false);
                            sendCode.setText("(" + integer + ")重新发送");
                        });
                    } else {
                        ToastUtils.error(httpResult.getMsg());
                    }
                });


    }


    /**
     * 确定修改
     *
     * @param view
     */
    public void confirm(View view) {
        if (Check.isEmpty(userName.get())) {
            ToastUtils.error("请输入手机号！");
            return;
        }
        if (!RegUtils.isMobile(userName.get())) {
            ToastUtils.error("手机号码格式不对！");
            return;
        }
        if (Check.isEmpty(code.get())) {
            ToastUtils.error("请输入验证码！");
            return;
        }
        if (Check.isEmpty(password.get())) {
            ToastUtils.error("请输入密码！");
            return;
        }
        if (!password.get().equals(password2.get())) {
            ToastUtils.error("密码不一致！");
            return;
        }

        mHttpRequest.createService(APIService.class)
                .forgetUserPwd(userName.get(), password.get(),code.get())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .doOnError(throwable -> {
                    throwable.printStackTrace();
                    LogUtil.e("找回密码异常 " + throwable.getMessage());
                })
                .subscribe(httpResult -> {
                    if (httpResult.getCode() == 200) {
                        LogUtil.e("找回密码成功 " + httpResult.getData());
                        ToastUtils.success("找回密码成功 ");
                        ((ForgetPwdActivity) view.getContext()).finish();
                    } else {
                        ToastUtils.error(httpResult.getMsg());
                    }
                });

    }


}
